package com.ccrfid.rmc6.service.business.rollcall2;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.business.rollcall2.*;
import com.ccrfid.rmc6.repository.business.rollcall2.*;
import com.ccrfid.rmc6.repository.poi.PoiLocationRepository;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.repository.sys.OrgRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.List;

@Service
@Transactional
public class Rollcall2ConfigServiceImpl implements Rollcall2ConfigService {
    private static final Logger logger = LoggerFactory.getLogger(Rollcall2ConfigServiceImpl.class);

    @Autowired
    private RcOrgGroupRepository rcOrgGroupRepository;
    @Autowired
    private RcOrgGroupMemberRepository rcOrgGroupMemberRepository;
    @Autowired
    private RcLocationGroupRepository rcLocationGroupRepository;
    @Autowired
    private RcLocationGroupMemberRepository rcLocationGroupMemberRepository;
    @Autowired
    private RcScheduleRepository rcScheduleRepository;
    @Autowired
    private OrgRepository orgRepository;
    @Autowired
    private PoiLocationRepository poiLocationRepository;

    @Override
    public Page<RcOrgGroupEntity> getRcOrgGroupPage(SearchCriteria criteria, Pageable pageable) {
        return rcOrgGroupRepository.search(criteria, pageable);
    }

    @Override
    public RcOrgGroupEntity getRcOrgGroupById(Long groupId) throws AppException {
        if (!rcOrgGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return rcOrgGroupRepository.getById(groupId);
    }

    @Override
    public RcOrgGroupEntity createRcOrgGroup(RcOrgGroupEntity entity) throws AppException {
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.group_name_is_null.getCode(), CustomErrors.group_name_is_null.getReason(), "");
        }
        return rcOrgGroupRepository.save(entity);
    }

    @Override
    public RcOrgGroupEntity updateRcOrgGroup(Long groupId, RcOrgGroupEntity entity) throws AppException {
        if (!rcOrgGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.group_name_is_null.getCode(), CustomErrors.group_name_is_null.getReason(), "");
        }
        entity.setId(groupId);
        return rcOrgGroupRepository.save(entity);
    }

    @Override
    public Boolean deleteRcOrgGroup(Long groupId) {
        //1.删除组员
        if (rcOrgGroupMemberRepository.existsByGroupId(groupId)) {
            rcOrgGroupMemberRepository.deleteAllByGroupId(groupId);
        }
        //2.删除组
        if (rcOrgGroupRepository.existsById(groupId)) {
            rcOrgGroupRepository.deleteById(groupId);
        }
        return true;
    }

    @Override
    public List<RcOrgGroupMemberEntity> getRcOrgGroupMembers(Long groupId) throws AppException {
        if (!rcOrgGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return rcOrgGroupMemberRepository.getAllByGroupId(groupId);
    }

    @Override
    public RcOrgGroupMemberEntity createRcOrgGroupMember(Long groupId, RcOrgGroupMemberEntity entity) throws AppException {
        if (!rcOrgGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (entity.getOrgId() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_id_is_null.getCode(), CustomErrors.org_id_is_null.getReason(), "");
        }
        if (!orgRepository.existsById(entity.getOrgId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_not_found.getCode(), CustomErrors.org_not_found.getReason(), "");
        }
        entity.setId(null);
        entity.setGroupId(groupId);
        return rcOrgGroupMemberRepository.save(entity);
    }

    @Override
    public Boolean deleteRcOrgGroupMember(Long groupId, Long memberId) throws AppException {
        if (rcOrgGroupMemberRepository.existsById(memberId)) {
            rcOrgGroupMemberRepository.deleteById(memberId);
        }
        return true;
    }

    @Override
    public Page<RcLocationGroupEntity> getRcLocationGroupPage(SearchCriteria criteria, Pageable pageable) {
        return rcLocationGroupRepository.search(criteria, pageable);
    }

    @Override
    public RcLocationGroupEntity getRcLocationGroupById(Long groupId) throws AppException {
        if (!rcLocationGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return rcLocationGroupRepository.getById(groupId);
    }

    @Override
    public RcLocationGroupEntity createRcLocationGroup(RcLocationGroupEntity entity) throws AppException {
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.group_name_is_null.getCode(), CustomErrors.group_name_is_null.getReason(), "");
        }
        return rcLocationGroupRepository.save(entity);
    }

    @Override
    public RcLocationGroupEntity updateRcLocationGroup(Long groupId, RcLocationGroupEntity entity) throws AppException {
        if (!rcLocationGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.group_name_is_null.getCode(), CustomErrors.group_name_is_null.getReason(), "");
        }
        entity.setId(groupId);
        return rcLocationGroupRepository.save(entity);
    }

    @Override
    public Boolean deleteRcLocationGroup(Long groupId) throws AppException {
        //1.删除组员
        if (rcLocationGroupMemberRepository.existsByGroupId(groupId)) {
            rcLocationGroupMemberRepository.deleteAllByGroupId(groupId);
        }
        //2.删除组
        if (rcLocationGroupRepository.existsById(groupId)) {
            rcLocationGroupRepository.deleteById(groupId);
        }
        return true;
    }

    @Override
    public List<RcLocationGroupMemberEntity> getRcLocationGroupMembers(Long groupId) throws AppException {
        if (!rcLocationGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return rcLocationGroupMemberRepository.getAllByGroupId(groupId);
    }

    @Override
    public RcLocationGroupMemberEntity createRcLocationGroupMember(Long groupId, RcLocationGroupMemberEntity entity)
            throws AppException {
        if (!rcLocationGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (entity.getLocationId() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.location_id_is_null.getCode(), CustomErrors.location_id_is_null.getReason(), "");
        }
        entity.setId(null);
        entity.setGroupId(groupId);
        return rcLocationGroupMemberRepository.save(entity);
    }

    @Override
    public Boolean deleteRcLocationGroupMember(Long groupId, Long memberId) throws AppException {
        if (rcLocationGroupMemberRepository.existsById(memberId)) {
            rcLocationGroupMemberRepository.deleteById(memberId);
        }
        return true;
    }

    @Override
    public Page<RcScheduleEntity> getRcSchedulePage(SearchCriteria criteria, Pageable pageable) {
        return rcScheduleRepository.search(criteria, pageable);
    }

    @Override
    public RcScheduleEntity getRcScheduleById(Long scheduleId) throws AppException {
        if (!rcScheduleRepository.existsById(scheduleId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return rcScheduleRepository.getById(scheduleId);
    }

    @Override
    public RcScheduleEntity createRcSchedule(RcScheduleEntity entity) throws AppException {
        if (entity.getScheduleTime() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.schedule_time_is_null.getCode(), CustomErrors.schedule_time_is_null.getReason(), "");
        }
        if (entity.getOrgGroupId() == null || entity.getOrgGroupName() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_id_is_null.getCode(), CustomErrors.org_id_is_null.getReason(), "");
        }
        if (!rcOrgGroupRepository.existsById(entity.getOrgGroupId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_group_not_found.getCode(), CustomErrors.org_group_not_found.getReason(), "");
        }
        if (entity.getLocationGroupId() == null || entity.getLocationGroupName() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.location_id_is_null.getCode(), CustomErrors.location_id_is_null.getReason(), "");
        }
        if (!rcLocationGroupRepository.existsById(entity.getLocationGroupId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.location_group_not_found.getCode(), CustomErrors.location_group_not_found.getReason(), "");
        }
        if (entity.getEnabled() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.enabled_is_null.getCode(), CustomErrors.enabled_is_null.getReason(), "");
        }
        return rcScheduleRepository.save(entity);
    }

    @Override
    public RcScheduleEntity updateRcSchedule(Long scheduleId, RcScheduleEntity entity) throws AppException {
        if (!rcScheduleRepository.existsById(scheduleId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (entity.getScheduleTime() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.schedule_time_is_null.getCode(), CustomErrors.schedule_time_is_null.getReason(), "");
        }
        if (entity.getOrgGroupId() == null || entity.getOrgGroupName() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_id_is_null.getCode(), CustomErrors.org_id_is_null.getReason(), "");
        }
        if (!rcOrgGroupRepository.existsById(entity.getOrgGroupId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.org_group_not_found.getCode(), CustomErrors.org_group_not_found.getReason(), "");
        }
        if (entity.getLocationGroupId() == null || entity.getLocationGroupName() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.location_id_is_null.getCode(), CustomErrors.location_id_is_null.getReason(), "");
        }
        if (!rcLocationGroupRepository.existsById(entity.getLocationGroupId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.location_group_not_found.getCode(), CustomErrors.location_group_not_found.getReason(), "");
        }
        if (entity.getEnabled() == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.enabled_is_null.getCode(), CustomErrors.enabled_is_null.getReason(), "");
        }
        entity.setId(scheduleId);
        return rcScheduleRepository.save(entity);
    }

    @Override
    public Boolean deleteRcScheduleById(Long scheduleId) throws AppException {
        if (rcScheduleRepository.existsById(scheduleId)) {
            rcScheduleRepository.deleteById(scheduleId);
        }
        return true;
    }
}
